package com.education.user.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.core.BaseController;
import com.education.common.poi.ExcelUtil;
import com.education.common.pojo.AjaxResult;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.DateUtils;
import com.education.user.pojo.SysBanner;
import com.education.user.pojo.SysBannerVO;
import com.education.user.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告位轮播 信息操作处理
 *
 * @author d
 * @date 2020-01-17
 */
@RestController
@RequestMapping("/k12main/sysBanner")
@CrossOrigin
public class SysBannerController extends BaseController {

    @Autowired
    private BannerService sysBannerService;


    /**
     * 查询广告位轮播列表
     */
    @PostMapping("/list")
    public ApiReturnObject list(@RequestBody SysBannerVO sysBanner) {
        Integer pageSize = sysBanner.getPageSize();
        Integer pageNum = sysBanner.getPageNum();
        if (pageSize !=null && pageNum != null){
            PageHelper.startPage(pageNum,pageSize);
        }
        SysBanner sysBanner1 = new SysBanner();
        sysBanner1.setIsdisplay("1");
        List<SysBanner> list = sysBannerService.selectSysBannerList(sysBanner1);
        PageInfo pages = new PageInfo<>(list);
        return new ApiReturnObject("200","查询成功",pages);
    }

    /**
     * 查询广告位轮播列表 门户显示
     */
    @PostMapping("/listShow")
    public ApiReturnObject listShow(@RequestBody SysBanner sysBanner) {
        sysBanner.setIsdisplay("1");
        List<SysBanner> list = sysBannerService.selectSysBannerList(sysBanner);
        return new ApiReturnObject("200","查询成功",list);
    }


    @GetMapping("/selectOne")
    public ApiReturnObject selectOne(String id) {
        SysBanner list = sysBannerService.selectOne(Integer.parseInt(id));
        return new ApiReturnObject("200","查询成功",list);
    }

    /**
     * 导出广告位轮播列表
     */
    @PostMapping("/export")
    public AjaxResult export(SysBanner sysBanner) {
        List<SysBanner> list = sysBannerService.selectSysBannerList(sysBanner);
        ExcelUtil<SysBanner> util = new ExcelUtil<SysBanner>(SysBanner. class);
        return util.exportExcel(list, "sysBanner");
    }

    /**
     * 升序
     */
    @GetMapping("/asc")
    public ApiReturnObject asc( String id) {
        if(id != null){
            SysBanner sysBanner = sysBannerService.selectOne(Integer.parseInt(id));
            if(sysBanner!=null){
                int sort = sysBanner.getSort();
                //将该记录排序之前的记录查询出来，根据sort降序取出第一条
                List<SysBanner> tAdmissionGuides = sysBannerService.selectSysBannerBySortAsc(Integer.toString(sort));
                if(tAdmissionGuides != null && tAdmissionGuides.size() > 0){
                    //将两个对象的排序字段互换
                    SysBanner tAdmissionGuide1 = tAdmissionGuides.get(0);
                    sysBanner.setSort(tAdmissionGuide1.getSort());
                    tAdmissionGuide1.setSort(sort);
                    sysBannerService.updateSysBanner(sysBanner);
                    sysBannerService.updateSysBanner(tAdmissionGuide1);
                    return new ApiReturnObject("200","更改排序成功","");
                }else {
                    return new ApiReturnObject("200","该记录已经是第一条","");
                }
            }
            return new ApiReturnObject("201","该记录不存在","");
        }
        return new ApiReturnObject("201","缺少参数","");
    }

    /**
     * 降序
     */
    @GetMapping("/desc")
    public ApiReturnObject desc(String id) {
        if(id != null){
            SysBanner sysBanner = sysBannerService.selectOne(Integer.parseInt(id));
            if(sysBanner!=null){
                int sort = sysBanner.getSort();
                //将该记录排序之前的记录查询出来，根据sort升序取出第一条
                List<SysBanner> tAdmissionGuides = sysBannerService.selectSysBannerBySortDesc(Integer.toString(sort));
                if(tAdmissionGuides != null && tAdmissionGuides.size() > 0){
                    //将两个对象的排序字段互换
                    SysBanner tAdmissionGuide1 = tAdmissionGuides.get(0);
                    sysBanner.setSort(tAdmissionGuide1.getSort());
                    tAdmissionGuide1.setSort(sort);
                    sysBannerService.updateSysBanner(sysBanner);
                    sysBannerService.updateSysBanner(tAdmissionGuide1);
                    return new ApiReturnObject("200","更改排序成功","");
                }else {
                    return new ApiReturnObject("200","该记录已经是最后一条","");
                }
            }
            return new ApiReturnObject("201","该记录不存在","");
        }
        return new ApiReturnObject("201","缺少参数","");
    }


    /**
     * 新增保存广告位轮播
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody SysBanner sysBanner) {
        if(sysBanner.getId()!=null){
            sysBanner.setId(null);
        }
        //判断文件的后缀名 jpg  png
        sysBanner.setCreateTime(DateUtils.FdateTime());
        List<SysBanner> sysBanners = sysBannerService.selectSysBannerList(new SysBanner());
        sysBanner.setSort(sysBanners.size()+1);
        sysBannerService.insertSysBanner(sysBanner);
        return new ApiReturnObject("200","添加成功","");
    }


    /**
     * 修改保存广告位轮播
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody SysBanner sysBanner) {
        sysBannerService.updateSysBanner(sysBanner);
        return new ApiReturnObject("200","修改成功","");
    }


    /**
     * 删除广告位轮播
     */
    @PostMapping("/del")
    public ApiReturnObject del(@RequestBody SysBanner sysBanner) {
        sysBannerService.deleteSysBannerById(sysBanner.getId().toString());
        return new ApiReturnObject("200","删除成功","");
    }

    /**
     * 删除广告位轮播
     */
    /*@PostMapping("/delAll")
    public ApiReturnObject remove(String ids) {
        sysBannerService.deleteSysBannerByIds(ids);
        return new ApiReturnObject("200","删除成功","");
    }*/
}